﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WhiteBoard
{
    public partial class BackGround : Form
    {
        //定义主菜单
        public MainMenu mainMenu;
     

        //定义左边弹出框中picturebox相关内容
        public static int boardSum=0;
        public static int boardNum = -1;
        public static int lastNum = -1;
        public static int pictureboxY=15;
        public static ArrayList pictureboxList = new ArrayList();
        public static ArrayList pageList = new ArrayList();
        public static ArrayList labelList = new ArrayList();
        public static ArrayList panelList = new ArrayList();
        public static int leftOfPanel0 = 15;


        public void CreateNewPage()
        {
            boardSum++;
            lastNum = boardNum;
            boardNum++;

            //pageList
            Bitmap tmpBmp = new Bitmap(this.pictureBox0.Image, this.pictureBox0.Size);
            pageList.Insert(boardNum,tmpBmp);
            //pageList.Add(tmpBmp);
            //tmpBmp.Dispose();

            //pictureboxList
            PictureBox tmpPicturebox = new PictureBox();
            tmpPicturebox.BorderStyle = BorderStyle.FixedSingle;
            tmpPicturebox.Left = 5;
            tmpPicturebox.Top =5;
            tmpPicturebox.Width = 150;
            tmpPicturebox.Height = 100;
            tmpPicturebox.Image = (Bitmap)pageList[boardNum];
            tmpPicturebox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            tmpPicturebox.MouseDown += new MouseEventHandler(Picturebox_MouseDown);
            tmpPicturebox.ContextMenuStrip = contextMenuStrip1;
            pictureboxList.Insert(boardNum,tmpPicturebox);
            //pictureboxList.Add(tmpPicturebox);
            //tmpPicturebox.Dispose();
            //this.bordPanel.Controls.Add((PictureBox)pictureboxList[boardNum - 1]);


            //labelList
            Label tmpLable = new Label();
            tmpLable.Text = boardSum.ToString();
            tmpLable.TextAlign = ContentAlignment.MiddleCenter;
            tmpLable.Left = 60;
            tmpLable.Top =  103;
            tmpLable.Width = 40;
            tmpLable.Height = 15;
            labelList.Insert(boardNum,tmpLable);
            //labelList.Add(tmpLable);
            //tmpLable.Dispose();
            //this.bordPanel.Controls.Add((Label)labelList[boardNum - 1]);

            //panelList
            Panel tmpPanel = new Panel();
            tmpPanel.Top = 10;
            //tmpPanel.Top = pictureboxY-5;
            tmpPanel.Width = 160;
            tmpPanel.Height = 120;
            tmpPanel.Controls.Add((PictureBox)pictureboxList[boardNum]);
            tmpPanel.Controls.Add((Label)labelList[boardNum]);
            panelList.Insert(boardNum,tmpPanel);
            //panelList.Add(tmpPanel);
            this.bordPanel.Controls.Add((Panel)panelList[boardNum]);
            //tmpPanel.Dispose();
            //pictureboxY += 120;
        }


        public void RefreshPage()
        {
            for (int i = boardNum; i != boardSum; i++)
            {
                Label tmpLabel = (Label)labelList[i];
                tmpLabel.Text =( i+1).ToString();
                Panel tmp = (Panel)panelList[i];
                if (i != 0)
                {
                    Panel tmp0 = (Panel)panelList[i - 1];
                    tmp.Left = tmp0.Left + 165;
                }
                else
                    tmp.Left = leftOfPanel0;
                //tmp.Top = 15 + i * 120;
            }
            this.pictureBox0.Image = (Bitmap)pageList[boardNum];
            Panel tmp2 = (Panel)panelList[boardNum];
            tmp2.BackColor = Color.Chocolate;
            if (lastNum >= 0&&lastNum!=boardNum)
            {
                Panel tmp3 = (Panel)panelList[lastNum];
                tmp3.BackColor = this.bordPanel.BackColor;
            }
        }

     

        public BackGround()
        {
            InitializeComponent();
            
            //初始化picturebox
          
            System.IO.FileStream fs;
            fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\BoardLibrary\white.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            this.pictureBox0.Image = System.Drawing.Image.FromStream(fs);
            fs.Close();
            this.CreateNewPage();
            this.RefreshPage();
           



            //实例化主菜单
            //toolForm = new ToolBar();
            //toolForm.Show(this);
            mainMenu = new MainMenu();
            mainMenu.Show(this);
        }

        //private void btnHide_Click(object sender, EventArgs e)
        //{
        //    if (this.bordPanel.Width == 5)
        //    {
        //        this.bordPanel.Width = 200;
        //    }
        //    else
        //        this.bordPanel.Width = 5;
        //}

        //private void BackGround_Paint(object sender, PaintEventArgs e)
        //{
        //    this.bordPanel.Height = 150;
        //    this.bordPanel.Width =this.Width;
        //    this.bordPanel.Left = 0;
        //    this.bordPanel.Top = this.Height - 150;
            

            //if (this.bordPanel.Width == 5)
            //{
            //    System.IO.FileStream fs;
            //    fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\icon\show.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            //    this.btnHide.BackgroundImage = System.Drawing.Image.FromStream(fs);
            //    fs.Close();
            //}
            //else
            //{
            //    System.IO.FileStream fs;
            //    fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\icon\hide.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            //    this.btnHide.BackgroundImage = System.Drawing.Image.FromStream(fs);
            //    fs.Close();
            //}
        //}

        private void Picturebox_MouseDown(object sender, EventArgs e)
        {
            //Panel lastPanel=(Panel)panelList[boardNum];
            //lastPanel.BackColor = this.bordPanel.BackColor;
            PictureBox tmp = (PictureBox)sender;
            lastNum = boardNum;
            boardNum=pictureboxList.IndexOf(tmp);
            //Panel tmpPanel = (Panel)panelList[boardNum];
            //tmpPanel.BackColor = Color.Chocolate;
            //tmpPanel.Focus();
            this.pictureBox0.Image = (Bitmap)pageList[boardNum];
            Panel tmp2 = (Panel)panelList[0];
            leftOfPanel0 = tmp2.Left;
            this.RefreshPage();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Panel tmp = (Panel)panelList[boardNum];
            tmp.Controls.Remove((PictureBox)pictureboxList[boardNum]);
            tmp.Controls.Remove((Label)labelList[boardNum]);
            this.bordPanel.Controls.Remove((Panel)panelList[boardNum]);
            pageList.RemoveAt(boardNum);
            pictureboxList.RemoveAt(boardNum);
            labelList.RemoveAt(boardNum);
            panelList.RemoveAt(boardNum);
           
            boardSum--;
            if(boardNum!=0)
                boardNum--;
            lastNum = boardNum;
            if (boardSum != 0)
                this.RefreshPage();
            else
            {
                boardNum = -1;
                lastNum = -1;
            }

        }

        private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage((Bitmap)pageList[boardNum]);
            Panel tmp = (Panel)panelList[boardNum];
            tmp.Controls.Remove((PictureBox)pictureboxList[boardNum]);
            tmp.Controls.Remove((Label)labelList[boardNum]);
            this.bordPanel.Controls.Remove((Panel)panelList[boardNum]);
            pageList.RemoveAt(boardNum);
            pictureboxList.RemoveAt(boardNum);
            labelList.RemoveAt(boardNum);
            panelList.RemoveAt(boardNum);

            boardSum--;
            if (boardNum != 0)
                boardNum--;
            lastNum = boardNum;
            if (boardSum != 0)
                this.RefreshPage();
            else
            {
                boardNum = -1;
                lastNum = -1;
            }
        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Clipboard.SetImage((Bitmap)pageList[boardNum]);
        }

        private void btnHide_Click(object sender, EventArgs e)
        {
            if (this.bordPanel.Height == 5)
            {
                this.btnHide.Top=this.Height-150;
                this.bordPanel.Top = this.Height -150;
                this.bordPanel.Height = 150;
                Bitmap bmp = new Bitmap(Resource1.hide);
                this.btnHide.BackgroundImage = bmp;
                //System.IO.FileStream fs;
                //fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\icon\show.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read);
                //this.btnHide.BackgroundImage = System.Drawing.Image.FromStream(fs);
                //fs.Close();
                
            }
            else
            {
                this.btnHide.Top = this.Height - 10;
                this.bordPanel.Top =this.Height- 5;
                this.bordPanel.Height = 5;
                Bitmap bmp = new Bitmap(Resource1.show);
                this.btnHide.BackgroundImage = bmp;
                //System.IO.FileStream fs;
                //fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\icon\hide.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read);
                //this.btnHide.BackgroundImage = System.Drawing.Image.FromStream(fs);
                //fs.Close();
            }
        }

        //private void btnHide_Paint(object sender, PaintEventArgs e)
        //{
        //    this.btnHide.Left = this.Width / 2 - 15;
        //    this.btnHide.Top = this.bordPanel.Top;
        //}

        private void BackGround_Load(object sender, EventArgs e)
        {
            this.bordPanel.Height = 150;
            this.bordPanel.Width =this.Width;
            this.bordPanel.Left = 0;
            this.bordPanel.Top = this.Height - 150;
            this.btnHide.Left = this.Width / 2 - 15;
            this.btnHide.Top = this.bordPanel.Top;
            Bitmap bmp = new Bitmap(Resource1.hide);
            this.btnHide.BackgroundImage = bmp;
        }

        //private void btnHide_Paint(object sender, PaintEventArgs e)
        //{
        //    if (this.bordPanel.Width == 5)
        //    {
        //        System.IO.FileStream fs;
        //        fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\BoardLibrary\show.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read);
        //        this.btnHide.BackgroundImage = System.Drawing.Image.FromStream(fs);
        //        fs.Close();
        //    }
        //    else
        //    {
        //        System.IO.FileStream fs;
        //        fs = new System.IO.FileStream(@"E:\程序\WhiteBoard\WhiteBoard\BoardLibrary\hide.bmp", System.IO.FileMode.Open, System.IO.FileAccess.Read);
        //        this.btnHide.BackgroundImage = System.Drawing.Image.FromStream(fs);
        //        fs.Close();
        //    }
                
        //}

    }
}
